package com.asktgapp.user.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.asktgapp.JGTool.JGColor;
import com.asktgapp.JGTool.Utils;
import com.asktgapp.R;
import com.asktgapp.application.ApiUtil;
import com.asktgapp.application.Apiservice;
import com.asktgapp.architecture.retrofit.ApiException;
import com.asktgapp.architecture.retrofit.ApiResponseBody;
import com.asktgapp.architecture.rxjava.ExceptionHandle;
import com.asktgapp.base.BaseFragment;
import com.asktgapp.base.SearchBaseActivity;
import com.asktgapp.eventbus.HomeEvent;
import com.asktgapp.model.FreeAskSearchVO;
import com.asktgapp.model.IndexVO;
import com.asktgapp.modulebase.common.adapter.BaseAdapter;
import com.asktgapp.modulebase.common.adapter.BaseViewHolder;
import com.asktgapp.modulebase.common.util.PreferencesUtil;
import com.asktgapp.modulebase.common.util.Util;
import com.asktgapp.user.activity.EngineerListActivity;
import com.asktgapp.user.activity.FreeAskActivity;
import com.asktgapp.user.activity.FreeAskAnswerDetailActivity;
import com.asktgapp.user.activity.HomeServiceActivity;
import com.asktgapp.user.activity.IndurstryInformationActivity;
import com.asktgapp.user.activity.MessageCenterActivity;
import com.asktgapp.user.activity.RentMachineNewActivity;
import com.asktgapp.user.activity.UsedSecondhandTypeActivity;
import com.asktgapp.utils.ImageDisplayUtil;
import com.asktgapp.widget.HomeBannerViewpPager;
import com.asktgapp.widget.RefreshRecyclerView;
import com.yanzhenjie.recyclerview.swipe.widget.DefaultItemDecoration;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * time:2018/1/22
 * Create By ZhaoKai
 * Email: dczk@foxmail.com
 * Description:
 */
@Deprecated
public class NewHomeFragment extends BaseFragment {

    private BaseAdapter mBaseAdapter;
    //搜索框
    @InjectView(R.id.et_search)
    EditText mSearch;
    //消息图标
    @InjectView(R.id.iv_message)
    ImageView mMessage;
    @InjectView(R.id.recycle)
    RefreshRecyclerView refreshRecyclerView;
    private RecyclerView mRecyclerView;
//    @InjectView(R.id.homeSRl)
//    SwipeRefreshLayout homeSRl;
    private int mBanHeight;
    private boolean needResume = false;
    private int mPage = 1;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_home, container, false);
        ButterKnife.inject(this, viewGroup);
        EventBus.getDefault().register(this);
        needResume = false;
        return super.onCreateView(inflater, viewGroup, savedInstanceState);
    }

    @Override
    protected void initView(View rootView) {
        WindowManager windowManager = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        mBanHeight = display.getWidth() * 1 /2;
        mRecyclerView = refreshRecyclerView.getRecyclerView();
        mSearch.setOnClickListener(this);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        mRecyclerView.addItemDecoration(new DefaultItemDecoration(JGColor.APP_BACKGROUND));

        refreshRecyclerView.setRefreshAction(new RefreshRecyclerView.Action() {
            @Override
            public void onAction(int page) {
                mPage = 1;
                homeBroadcast();
            }
        });
//        refreshRecyclerView.setLoadMoreAction(new RefreshRecyclerView.Action() {
//            @Override
//            public void onAction(int page) {
//                mPage = page;
//                selProbleList();
//            }
//        });


        mBaseAdapter = new BaseAdapter(getActivity()) {
            @Override
            public BaseViewHolder getViewHolder(ViewGroup parent, int viewType) {
                BaseViewHolder holder;
                if (viewType == 0) {
                    holder = new BaseViewHolder(LayoutInflater.from(getActivity()).inflate(R.layout.item_free_ask_hot, parent, false),
                            new BaseViewHolder.Callbacks2() {
                                @Override
                                public void bind(BaseViewHolder holder, Object data) {
                                    if (data instanceof FreeAskSearchVO) {
                                        final FreeAskSearchVO bean = (FreeAskSearchVO) data;


                                        LinearLayout allAnswer = holder.getView(R.id.llAnswerComment);
                                        ImageView askHead = holder.getView(R.id.iv_head_ask);
//                                    ImageView answerHead = holder.getView(R.id.iv_answer_head);
                                        ImageView ivZan = holder.getView(R.id.ivZan);

                                        ImageDisplayUtil.loadCircleHeadImage(getActivity(), askHead, bean.getUserPic());
                                        holder.setText(R.id.tvAskName, bean.getUsername());
                                        holder.setText(R.id.tvZan, bean.getProblemLikeNum());
                                        holder.setText(R.id.tv_answer, bean.getContent());

                                        Date date = Util.StrToDate(bean.getAddTime(), "yyyy.MM.dd HH:mm:ss");
                                        String timeStr = Utils.getAskTime(date);
                                        holder.setText(R.id.addTime, timeStr);

                                        if (bean.getAnswerList().size() > 0) {
                                            holder.getView(R.id.llAnswerComment).setVisibility(View.VISIBLE);
                                            holder.setText(R.id.answerNumber, bean.getAnswerNum() + " 回复");
//                                        ImageDisplayUtil.loadCircleHeadImage(getActivity(), answerHead, bean.getUserPic());
                                            holder.setText(R.id.tvAnswerName, bean.getAnswerList().get(0).getUsername1());
                                            holder.setText(R.id.tvAnswerContent, bean.getAnswerList().get(0).getContent());
                                            //判断是否工程师
                                            if (bean.getAnswerList().get(0).getIsProjector().equals("1")) {
                                                holder.getView(R.id.tvCommentType).setVisibility(View.VISIBLE);
                                            } else {
                                                holder.getView(R.id.tvCommentType).setVisibility(View.INVISIBLE);
                                            }
                                            if (bean.getAnswerList().size() > 1) {
                                                holder.getView(R.id.llAnswer2).setVisibility(View.VISIBLE);
                                                holder.setText(R.id.tvAnswerName2, bean.getAnswerList().get(1).getUsername1());
                                                holder.setText(R.id.tvAnswerContent2, bean.getAnswerList().get(1).getContent());
                                                //判断是否工程师
                                                if (bean.getAnswerList().get(1).getIsProjector().equals("1")) {
                                                    holder.getView(R.id.tvCommentType2).setVisibility(View.VISIBLE);
                                                } else {
                                                    holder.getView(R.id.tvCommentType2).setVisibility(View.INVISIBLE);
                                                }
                                            } else {
                                                holder.getView(R.id.llAnswer2).setVisibility(View.GONE);
                                            }

                                        } else {
                                            holder.setText(R.id.answerNumber, "无回复");
                                            holder.getView(R.id.llAnswerComment).setVisibility(View.GONE);
                                            holder.getView(R.id.llAnswer2).setVisibility(View.GONE);
                                        }
                                        allAnswer.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                FreeAskAnswerDetailActivity.startFreeAskAnswerDetailActivty( bean.getId(),getContext());
                                            }
                                        });
                                        if (TextUtils.isEmpty(bean.getLikeId()))
                                            ivZan.setImageResource(R.mipmap.zan_2);
                                        else {
                                            ivZan.setImageResource(R.mipmap.zan_1);
                                        }
                                        holder.getView(R.id.zan).setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                //点赞接口
                                                if (TextUtils.isEmpty(bean.getLikeId())) {
                                                    dealZan(bean.getId(), "1", bean);
                                                } else {
                                                    dealZan(bean.getId(), "0", bean);
                                                }
                                                index = mBaseAdapter.getItemIndex(bean);
                                            }
                                        });
                                    }
                                }

                                @Override
                                public void onItemClick(BaseViewHolder holder, View view, int position) {
                                    FreeAskSearchVO b = (FreeAskSearchVO) mBaseAdapter.getData(position);
                                    FreeAskAnswerDetailActivity.startFreeAskAnswerDetailActivty( b.getId(),getContext());
                                }
                            });

                } else if (viewType == 1) {
                    holder = new BaseViewHolder(LayoutInflater.from(getActivity()).inflate(R.layout.item_free_ask_img1, parent, false),
                            new BaseViewHolder.Callbacks2() {
                                @Override
                                public void bind(BaseViewHolder holder, Object data) {
                                    if (data instanceof FreeAskSearchVO) {
                                        final FreeAskSearchVO bean = (FreeAskSearchVO) data;


                                        LinearLayout allAnswer = holder.getView(R.id.llAnswerComment);
                                        ImageView askHead = holder.getView(R.id.iv_head_ask);
//                                    ImageView answerHead = holder.getView(R.id.iv_answer_head);
                                        ImageView ivZan = holder.getView(R.id.ivZan);
                                        ImageView ivQuestionImg1 = holder.getView(R.id.ivQuestionImg1);

                                        ImageDisplayUtil.loadCircleHeadImage(getActivity(), askHead, bean.getUserPic());
                                        holder.setText(R.id.tvAskName, bean.getUsername());
                                        holder.setText(R.id.tvZan, bean.getProblemLikeNum());
                                        holder.setText(R.id.tv_answer, bean.getContent());

                                        ImageDisplayUtil.LoadASKIMG(ivQuestionImg1, getActivity(), bean.getImgList().get(0));

                                        Date date = Util.StrToDate(bean.getAddTime(), "yyyy.MM.dd HH:mm:ss");
                                        String timeStr = Utils.getAskTime(date);
                                        holder.setText(R.id.addTime, timeStr);

                                        if (bean.getAnswerList().size() > 0) {
                                            holder.getView(R.id.llAnswerComment).setVisibility(View.VISIBLE);
                                            holder.setText(R.id.answerNumber, bean.getAnswerNum() + " 回复");
//                                        ImageDisplayUtil.loadCircleHeadImage(getActivity(), answerHead, bean.getUserPic());
                                            holder.setText(R.id.tvAnswerName, bean.getAnswerList().get(0).getUsername1());
                                            holder.setText(R.id.tvAnswerContent, bean.getAnswerList().get(0).getContent());
                                            //判断是否工程师
                                            if (bean.getAnswerList().get(0).getIsProjector().equals("1")) {
                                                holder.getView(R.id.tvCommentType).setVisibility(View.VISIBLE);
                                            } else {
                                                holder.getView(R.id.tvCommentType).setVisibility(View.INVISIBLE);
                                            }
                                            if (bean.getAnswerList().size() > 1) {
                                                holder.getView(R.id.llAnswer2).setVisibility(View.VISIBLE);
                                                holder.setText(R.id.tvAnswerName2, bean.getAnswerList().get(1).getUsername1());
                                                holder.setText(R.id.tvAnswerContent2, bean.getAnswerList().get(1).getContent());
                                                //判断是否工程师
                                                if (bean.getAnswerList().get(1).getIsProjector().equals("1")) {
                                                    holder.getView(R.id.tvCommentType2).setVisibility(View.VISIBLE);
                                                } else {
                                                    holder.getView(R.id.tvCommentType2).setVisibility(View.INVISIBLE);
                                                }
                                            } else {
                                                holder.getView(R.id.llAnswer2).setVisibility(View.GONE);
                                            }

                                        } else {
                                            holder.setText(R.id.answerNumber, "无回复");
                                            holder.getView(R.id.llAnswerComment).setVisibility(View.GONE);
                                            holder.getView(R.id.llAnswer2).setVisibility(View.GONE);
                                        }
                                        allAnswer.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                FreeAskAnswerDetailActivity.startFreeAskAnswerDetailActivty( bean.getId(),getContext());
                                            }
                                        });
                                        if (TextUtils.isEmpty(bean.getLikeId()))
                                            ivZan.setImageResource(R.mipmap.zan_2);
                                        else {
                                            ivZan.setImageResource(R.mipmap.zan_1);
                                        }
                                        holder.getView(R.id.zan).setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                //点赞接口
                                                if (TextUtils.isEmpty(bean.getLikeId())) {
                                                    dealZan(bean.getId(), "1", bean);
                                                } else {
                                                    dealZan(bean.getId(), "0", bean);
                                                }
                                                index = mBaseAdapter.getItemIndex(bean);
                                            }
                                        });
                                    }
                                }

                                @Override
                                public void onItemClick(BaseViewHolder holder, View view, int position) {
                                    FreeAskSearchVO b = (FreeAskSearchVO) mBaseAdapter.getData(position);
                                    FreeAskAnswerDetailActivity.startFreeAskAnswerDetailActivty( b.getId(),getContext());
                                }
                            });

                } else if (viewType == 2) {
                    holder = new BaseViewHolder(LayoutInflater.from(getActivity()).inflate(R.layout.item_free_ask_img2, parent, false),
                            new BaseViewHolder.Callbacks2() {
                                @Override
                                public void bind(BaseViewHolder holder, Object data) {
                                    if (data instanceof FreeAskSearchVO) {
                                        final FreeAskSearchVO bean = (FreeAskSearchVO) data;


                                        LinearLayout allAnswer = holder.getView(R.id.llAnswerComment);
                                        ImageView askHead = holder.getView(R.id.iv_head_ask);
//                                    ImageView answerHead = holder.getView(R.id.iv_answer_head);
                                        ImageView ivZan = holder.getView(R.id.ivZan);
                                        ImageView ivQuestionImg1 = holder.getView(R.id.ivQuestionImg1);
                                        ImageView ivQuestionImg2 = holder.getView(R.id.ivQuestionImg2);
                                        LinearLayout llQuestionImg = holder.getView(R.id.llQuestionImg);
                                        ImageDisplayUtil.loadCircleHeadImage(getActivity(), askHead, bean.getUserPic());
                                        holder.setText(R.id.tvAskName, bean.getUsername());
                                        holder.setText(R.id.tvZan, bean.getProblemLikeNum());
                                        holder.setText(R.id.tv_answer, bean.getContent());

                                        llQuestionImg.setVisibility(View.VISIBLE);
                                        ImageDisplayUtil.LoadASKIMG(ivQuestionImg1, getActivity(), bean.getImgList().get(0));
                                        ImageDisplayUtil.LoadASKIMG(ivQuestionImg2, getActivity(), bean.getImgList().get(1));

                                        Date date = Util.StrToDate(bean.getAddTime(), "yyyy.MM.dd HH:mm:ss");
                                        String timeStr = Utils.getAskTime(date);
                                        holder.setText(R.id.addTime, timeStr);

                                        if (bean.getAnswerList().size() > 0) {
                                            holder.getView(R.id.llAnswerComment).setVisibility(View.VISIBLE);
                                            holder.setText(R.id.answerNumber, bean.getAnswerNum() + " 回复");
//                                        ImageDisplayUtil.loadCircleHeadImage(getActivity(), answerHead, bean.getUserPic());
                                            holder.setText(R.id.tvAnswerName, bean.getAnswerList().get(0).getUsername1());
                                            holder.setText(R.id.tvAnswerContent, bean.getAnswerList().get(0).getContent());
                                            //判断是否工程师
                                            if (bean.getAnswerList().get(0).getIsProjector().equals("1")) {
                                                holder.getView(R.id.tvCommentType).setVisibility(View.VISIBLE);
                                            } else {
                                                holder.getView(R.id.tvCommentType).setVisibility(View.INVISIBLE);
                                            }
                                            if (bean.getAnswerList().size() > 1) {
                                                holder.getView(R.id.llAnswer2).setVisibility(View.VISIBLE);
                                                holder.setText(R.id.tvAnswerName2, bean.getAnswerList().get(1).getUsername1());
                                                holder.setText(R.id.tvAnswerContent2, bean.getAnswerList().get(1).getContent());
                                                //判断是否工程师
                                                if (bean.getAnswerList().get(1).getIsProjector().equals("1")) {
                                                    holder.getView(R.id.tvCommentType2).setVisibility(View.VISIBLE);
                                                } else {
                                                    holder.getView(R.id.tvCommentType2).setVisibility(View.INVISIBLE);
                                                }
                                            } else {
                                                holder.getView(R.id.llAnswer2).setVisibility(View.GONE);
                                            }

                                        } else {
                                            holder.setText(R.id.answerNumber, "无回复");
                                            holder.getView(R.id.llAnswerComment).setVisibility(View.GONE);
                                            holder.getView(R.id.llAnswer2).setVisibility(View.GONE);
                                        }
                                        allAnswer.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                FreeAskAnswerDetailActivity.startFreeAskAnswerDetailActivty( bean.getId(),getContext());
                                            }
                                        });
                                        if (TextUtils.isEmpty(bean.getLikeId()))
                                            ivZan.setImageResource(R.mipmap.zan_2);
                                        else {
                                            ivZan.setImageResource(R.mipmap.zan_1);
                                        }
                                        holder.getView(R.id.zan).setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                //点赞接口
                                                if (TextUtils.isEmpty(bean.getLikeId())) {
                                                    dealZan(bean.getId(), "1", bean);
                                                } else {
                                                    dealZan(bean.getId(), "0", bean);
                                                }
                                                index = mBaseAdapter.getItemIndex(bean);
                                            }
                                        });
                                    }
                                }

                                @Override
                                public void onItemClick(BaseViewHolder holder, View view, int position) {
                                    FreeAskSearchVO b = (FreeAskSearchVO) mBaseAdapter.getData(position);
                                    FreeAskAnswerDetailActivity.startFreeAskAnswerDetailActivty( b.getId(),getContext());
                                }
                            });
                } else if(viewType==3) {
                    holder = new BaseViewHolder(LayoutInflater.from(getActivity()).inflate(R.layout.item_free_ask_img3, parent, false),
                            new BaseViewHolder.Callbacks2() {
                                @Override
                                public void bind(BaseViewHolder holder, Object data) {
                                    if (data instanceof FreeAskSearchVO) {
                                        final FreeAskSearchVO bean = (FreeAskSearchVO) data;


                                        LinearLayout allAnswer = holder.getView(R.id.llAnswerComment);
                                        ImageView askHead = holder.getView(R.id.iv_head_ask);
//                                    ImageView answerHead = holder.getView(R.id.iv_answer_head);
                                        ImageView ivZan = holder.getView(R.id.ivZan);
                                        ImageView ivQuestionImg1 = holder.getView(R.id.ivQuestionImg1);
                                        ImageView ivQuestionImg2 = holder.getView(R.id.ivQuestionImg2);
                                        ImageView ivQuestionImg3 = holder.getView(R.id.ivQuestionImg3);
                                        LinearLayout llQuestionImg = holder.getView(R.id.llQuestionImg);
                                        ImageDisplayUtil.loadCircleHeadImage(getActivity(), askHead, bean.getUserPic());
                                        holder.setText(R.id.tvAskName, bean.getUsername());
                                        holder.setText(R.id.tvZan, bean.getProblemLikeNum());
                                        holder.setText(R.id.tv_answer, bean.getContent());

                                        llQuestionImg.setVisibility(View.VISIBLE);

                                        ImageDisplayUtil.LoadASKIMG(ivQuestionImg1, getActivity(), bean.getImgList().get(0));
                                        ImageDisplayUtil.LoadASKIMG(ivQuestionImg2, getActivity(), bean.getImgList().get(1));
                                        ImageDisplayUtil.LoadASKIMG(ivQuestionImg3, getActivity(), bean.getImgList().get(2));


                                        Date date = Util.StrToDate(bean.getAddTime(), "yyyy.MM.dd HH:mm:ss");
                                        String timeStr = Utils.getAskTime(date);
                                        holder.setText(R.id.addTime, timeStr);

                                        if (bean.getAnswerList().size() > 0) {
                                            holder.getView(R.id.llAnswerComment).setVisibility(View.VISIBLE);
                                            holder.setText(R.id.answerNumber, bean.getAnswerNum() + " 回复");
//                                        ImageDisplayUtil.loadCircleHeadImage(getActivity(), answerHead, bean.getUserPic());
                                            holder.setText(R.id.tvAnswerName, bean.getAnswerList().get(0).getUsername1());
                                            holder.setText(R.id.tvAnswerContent, bean.getAnswerList().get(0).getContent());
                                            //判断是否工程师
                                            if (bean.getAnswerList().get(0).getIsProjector().equals("1")) {
                                                holder.getView(R.id.tvCommentType).setVisibility(View.VISIBLE);
                                            } else {
                                                holder.getView(R.id.tvCommentType).setVisibility(View.INVISIBLE);
                                            }
                                            if (bean.getAnswerList().size() > 1) {
                                                holder.getView(R.id.llAnswer2).setVisibility(View.VISIBLE);
                                                holder.setText(R.id.tvAnswerName2, bean.getAnswerList().get(1).getUsername1());
                                                holder.setText(R.id.tvAnswerContent2, bean.getAnswerList().get(1).getContent());
                                                //判断是否工程师
                                                if (bean.getAnswerList().get(1).getIsProjector().equals("1")) {
                                                    holder.getView(R.id.tvCommentType2).setVisibility(View.VISIBLE);
                                                } else {
                                                    holder.getView(R.id.tvCommentType2).setVisibility(View.INVISIBLE);
                                                }
                                            } else {
                                                holder.getView(R.id.llAnswer2).setVisibility(View.GONE);
                                            }

                                        } else {
                                            holder.setText(R.id.answerNumber, "无回复");
                                            holder.getView(R.id.llAnswerComment).setVisibility(View.GONE);
                                            holder.getView(R.id.llAnswer2).setVisibility(View.GONE);
                                        }
                                        allAnswer.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                FreeAskAnswerDetailActivity.startFreeAskAnswerDetailActivty( bean.getId(),getContext());
                                            }
                                        });
                                        if (TextUtils.isEmpty(bean.getLikeId()))
                                            ivZan.setImageResource(R.mipmap.zan_2);
                                        else {
                                            ivZan.setImageResource(R.mipmap.zan_1);
                                        }
                                        holder.getView(R.id.zan).setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                //点赞接口
                                                if (TextUtils.isEmpty(bean.getLikeId())) {
                                                    dealZan(bean.getId(), "1", bean);
                                                } else {
                                                    dealZan(bean.getId(), "0", bean);
                                                }
                                                index = mBaseAdapter.getItemIndex(bean);
                                            }
                                        });
                                    }
                                }

                                @Override
                                public void onItemClick(BaseViewHolder holder, View view, int position) {
                                    FreeAskSearchVO b = (FreeAskSearchVO) mBaseAdapter.getData(position);
                                    FreeAskAnswerDetailActivity.startFreeAskAnswerDetailActivty( b.getId(),getContext());
                                }
                            });
                }else {
                    holder = new BaseViewHolder(LayoutInflater.from(getActivity()).inflate(R.layout.item_home_head, parent, false),
                            new BaseViewHolder.Callbacks2() {
                                @Override
                                public void bind(BaseViewHolder holder, Object data) {
                                    if (data instanceof FreeAskSearchVO) {
                                        HomeBannerViewpPager mBanner = holder.getView(R.id.banne);
                                        if (indexVO != null && indexVO.getBannerList().size() > 0) {
                                            mBanner.setVisibility(View.VISIBLE);
                                            mBanner.setH(mBanHeight);
//                                            mBanner.loadBannerData(indexVO.getBannerList());
                                        } else {
                                            mBanner.setVisibility(View.GONE);
                                        }
                                        if (indexVO != null) {
                                            holder.setText(R.id.tv_des1, indexVO.getQuestionInfo());
                                            holder.setText(R.id.tv_des2, indexVO.getProjectInfo());
                                        }
                                        holder.getView(R.id.tv_item1).setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {//二手设备
                                                startActivity(new Intent(getActivity(), UsedSecondhandTypeActivity.class));
                                            }
                                        });
                                        holder.getView(R.id.tv_item2).setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {//设备出租
                                                startActivity(new Intent(getActivity(), RentMachineNewActivity.class));
                                            }
                                        });
                                        holder.getView(R.id.tv_item3).setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {//维修常识
                                                startActivity(new Intent(getActivity(), IndurstryInformationActivity.class));
                                            }
                                        });
                                        holder.getView(R.id.tv_item5).setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {//上门服务
                                                if (checkIsLogin("登录后可查看更多内容"))
                                                    startActivity(new Intent(getActivity(), HomeServiceActivity.class));
                                            }
                                        });
                                        holder.getView(R.id.rl_free_ask).setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {//快速提问
                                                startActivity(new Intent(getActivity(), FreeAskActivity.class));
                                            }
                                        });
                                        holder.getView(R.id.rl_ask_engineer).setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {//咨询工程师
                                                if (checkIsLogin("登录后可查看更多内容"))
                                                    startActivity(new Intent(getActivity(), EngineerListActivity.class));
                                            }
                                        });
                                        holder.getView(R.id.tv_home_more_question).setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                startActivity(new Intent(getActivity(), FreeAskActivity.class));
                                            }
                                        });
                                    }
                                }

                                @Override
                                public void onItemClick(BaseViewHolder holder, View view, int position) {

                                }
                            });
                }
                return holder;
            }

            @Override
            public int getItemViewType(int position) {
                FreeAskSearchVO b = (FreeAskSearchVO) mBaseAdapter.getData().get(position);
                if (b.isHead())
                    return 4;//头部布局
                else {
                    if (b.getImgList().size() == 1)
                        return 1;
                    if (b.getImgList().size() == 2)
                        return 2;
                    if (b.getImgList().size() == 3)
                        return 3;
                    if (b.getImgList().size() == 0)
                        return 0;
                }
                return super.getItemViewType(position);
            }
        };





        mRecyclerView.setAdapter(mBaseAdapter);
        List<FreeAskSearchVO> freeAskSearchVOS = new ArrayList<>();
        FreeAskSearchVO freeAskSearchVO = new FreeAskSearchVO();
        freeAskSearchVO.setHead(true);
        freeAskSearchVOS.add(freeAskSearchVO);
        mBaseAdapter.setData(freeAskSearchVOS);
    }

    private IndexVO indexVO;

    @Override
    public void loadData() {
//        visibleLoading();
        showProgress(getClass().getSimpleName());
        homeBroadcast();
        needResume = true;

    }


    //问答列表
    private void selProbleList() {
        Apiservice apiservice = ApiUtil.getInstance().create();
        HashMap params = new HashMap();
        if (!TextUtils.isEmpty(PreferencesUtil.getString(getContext(), PreferencesUtil.USER_ID)))
            params.put("userId", PreferencesUtil.getString(getContext(), PreferencesUtil.USER_ID));

        params.put("page", 1);
        params.put("isHot", "1");
        Call<ApiResponseBody<List<FreeAskSearchVO>>> call = apiservice.selProbleList(params);
        call.enqueue(new Callback<ApiResponseBody<List<FreeAskSearchVO>>>() {
            @Override
            public void onResponse(Call<ApiResponseBody<List<FreeAskSearchVO>>> call, Response<ApiResponseBody<List<FreeAskSearchVO>>> response) {
                List<FreeAskSearchVO> bean;
                if (response.isSuccessful()) {
                    bean = response.body().getResult();
                    if (bean != null && bean.size() > 0) {
                        if (bean.size() == 0) {
                            refreshRecyclerView.showNoMore();
                        }

                    }else{
                        bean = new ArrayList<>();
                    }

                } else {
                    showTost(response.raw().message(), 1);
                    bean = new ArrayList<>();
                }
                FreeAskSearchVO freeAskSearchVO = new FreeAskSearchVO();
                freeAskSearchVO.setHead(true);
                bean.add(0, freeAskSearchVO);
                mBaseAdapter.setData(bean);
                refreshRecyclerView.endPage();
                dismissProgress();
            }

            @Override
            public void onFailure(Call<ApiResponseBody<List<FreeAskSearchVO>>> call, Throwable t) {
                mBaseAdapter.notifyDataSetChanged();
                dismissProgress();
                ApiException apiException = ExceptionHandle.handleException(t);
                if (apiException.isTokenInvalid()) {
                    showUnLoginSnackbar();
                } else if (apiException.isNetConnected()) {
                    showSetNetworkSnackbar();
                } else {
                    showTost(apiException.getMessage(), 0);
                }
            }
        });
    }

    private int index = -1;

    //点赞接口
    private void dealZan(String id, final String temp, final FreeAskSearchVO bean) {
        if (Util.isEmpty(PreferencesUtil.getString(getActivity(), PreferencesUtil.USER_ID))) {
            showTost("未登录，不能点赞！请先登录", 1);
            return;
        }
        Apiservice apiservice = ApiUtil.getInstance().create();
        HashMap params = new HashMap();

        params.put("temp", temp);
        params.put("type", 2);
        params.put("id", id);
        params.put("userid", PreferencesUtil.getString(getActivity(), PreferencesUtil.USER_ID));
        showProgress(getClass().getSimpleName());
        Call<ApiResponseBody> call = apiservice.doLike(params);
        call.enqueue(new Callback<ApiResponseBody>() {
            @Override
            public void onResponse(Call<ApiResponseBody> call, Response<ApiResponseBody> response) {
                dismissProgress();
                if (response.isSuccessful()) {
                    int num = Integer.valueOf(bean.getProblemLikeNum());
                    if (temp.equals("1")) {
                        num++;
                        bean.setLikeId("13");
                    } else {
                        num--;
                        bean.setLikeId(null);
                    }
                    bean.setProblemLikeNum(String.valueOf(num));
                    mBaseAdapter.update(bean, index);
                } else {
                    showTost(response.raw().message(), 0);
                }
            }

            @Override
            public void onFailure(Call<ApiResponseBody> call, Throwable t) {
                dismissProgress();
                ApiException exception = ExceptionHandle.handleException(t);
                if (exception.isTokenInvalid()) {
                    showUnLoginSnackbar();
                } else if (exception.isNetConnected()) {
                    showSetNetworkSnackbar();
                } else {
                    showTost(exception.getMessage(), 1);
                }
            }
        });

    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void hasHomeEvent(HomeEvent event) {

        if (checkIsLogin("登录后可查看更多内容")) {
//            if (event.getType() == 3) {
//                startActivity(new Intent(getActivity(), EngineerListActivity.class));
//            } else if (event.getType() == 4) {
//                startActivity(new Intent(getActivity(), HomeServiceActivity.class));
//            }
        }
    }

    @Override
    public void onDestroyView() {
        ButterKnife.reset(this);
        super.onDestroyView();

    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onResume() {
        super.onResume();
//        if (needResume)
//            selProbleList();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            //消息中心
            case R.id.iv_message:
                startActivity(new Intent(getActivity(), MessageCenterActivity.class));
                break;
            //搜索
            case R.id.et_search:
                startActivity(new Intent(getActivity(), SearchBaseActivity.class));
                break;
        }

    }

    public void homeBroadcast() {
        Apiservice apiservice = ApiUtil.getInstance().create();
        HashMap params = new HashMap();
        Call<ApiResponseBody<IndexVO>> call = apiservice.homeBroadcast(params);
        call.enqueue(new Callback<ApiResponseBody<IndexVO>>() {
            @Override
            public void onResponse(Call<ApiResponseBody<IndexVO>> call, Response<ApiResponseBody<IndexVO>> response) {
                if (response.isSuccessful()) {
                    Log.i("jgapp", "success" + System.currentTimeMillis() + "");
                    indexVO = response.body().getResult();
                } else {
                    showTost(response.raw().message(), 0);
                }
                selProbleList();
            }

            @Override
            public void onFailure(Call<ApiResponseBody<IndexVO>> call, Throwable t) {
//                ApiException apiException = ExceptionHandle.handleException(t);
//                showTost(apiException.getMessage(), 1);
                selProbleList();
            }
        });
    }
}
